
package com.xuelangyun.form.common.utils;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;

import com.xuelangyun.form.common.xss.SQLFilter;
import org.apache.commons.lang.StringUtils;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 查询参数
 *
 * @author weiqing.hk
 * @since 2.0.0 2017-03-14
 */
public class Query<T> extends LinkedHashMap<String, Object> {
	private static final long serialVersionUID = 1L;
    /**
     * mybatis-plus分页参数
     */
    private Page<T> page;
    /**
     * 当前页码
     */
    private int currPage = 1;
    /**
     * 每页条数
     */
    private int limit = 10;
    
    
    public Query(Integer pageNum , Integer pageSize ){
    	
    	if(pageNum == null) {
    		pageNum = currPage;
    	}
    	
    	if(pageSize == null) {
    		pageSize = limit;
    	}
    	
    	 //分页参数
    	this.currPage = pageNum;
    	this.limit = pageSize;
       

        this.put("offset", (currPage - 1) * limit);
        this.put("page", currPage);
        this.put("limit", limit);

        //防止SQL注入（因为sidx、order是通过拼接SQL实现排序的，会有SQL注入风险）
      

        //mybatis-plus分页
        this.page = new Page<>(currPage, limit);

         
    }
    
    public Query(Integer pageNum , Integer pageSize , String orderField , Boolean asc){
    	
    	if(pageNum == null) {
    		pageNum = currPage;
    	}
    	
    	if(pageSize == null) {
    		pageSize = limit;
    	}
    	
    	 //分页参数
    	this.currPage = pageNum;
    	this.limit = pageSize;
       

        this.put("offset", (currPage - 1) * limit);
        this.put("page", currPage);
        this.put("limit", limit);

        //防止SQL注入（因为sidx、order是通过拼接SQL实现排序的，会有SQL注入风险）
      

        //mybatis-plus分页
        this.page = new Page<>(currPage, limit);

        //排序
        if(StringUtils.isNotBlank(orderField)){
        	
        	OrderItem oi = new OrderItem();
        	oi.setColumn(orderField);
        	if(asc != null)
        		oi.setAsc(asc);
        	
        	
            this.page.setOrders(Arrays.asList(oi));
            
        }
    	
    }
    
    

    public Query(Map<String, Object> params){
        this.putAll(params);

        //分页参数
        if(params.get("page") != null){
        	
        	Object page = params.get("page");
        	 
        	currPage = Integer.parseInt(page.toString());
        	 
        	
          
        }
        if(params.get("limit") != null){
        	Object limit_ = params.get("limit");
        	 
            limit = Integer.parseInt(limit_.toString());
        }

        this.put("offset", (currPage - 1) * limit);
        this.put("page", currPage);
        this.put("limit", limit);

        //防止SQL注入（因为sidx、order是通过拼接SQL实现排序的，会有SQL注入风险）
        String sidx = SQLFilter.sqlInject((String)params.get("sidx"));
        String order = SQLFilter.sqlInject((String)params.get("order"));
        this.put("sidx", sidx);
        this.put("order", order);

        //mybatis-plus分页
        this.page = new Page<>(currPage, limit);

        //排序
        if(StringUtils.isNotBlank(sidx) && StringUtils.isNotBlank(order)){
        	
        	OrderItem oi = new OrderItem();
        	oi.setColumn(sidx);
        	if("ASC".equalsIgnoreCase(order))
        		oi.setAsc(true);
        	
        	
            this.page.setOrders(Arrays.asList(oi));
        	
//            this.page.setOrderByField(sidx);
//            this.page.setAsc("ASC".equalsIgnoreCase(order));
        }

    }
    
    public Query(PageUtils pu){
       

        //分页参数
       
        	 
        	currPage = (int) pu.getCurrPage();
         
       
        	 
            limit = (int) pu.getPageSize();
        

        this.put("offset", (currPage - 1) * limit);
        this.put("page", currPage);
        this.put("limit", limit);

        //防止SQL注入（因为sidx、order是通过拼接SQL实现排序的，会有SQL注入风险）
       String orderField = pu.getOrderField();
       Boolean asc = pu.getAsc();
        

        //mybatis-plus分页
        this.page = new Page<>(currPage, limit);

        //排序
        if(StringUtils.isNotBlank(orderField) && asc != null){
        	
        	OrderItem oi = new OrderItem();
        	oi.setColumn(orderField);
        	oi.setAsc(asc);
        	
        	
            this.page.setOrders(Arrays.asList(oi));
        	
//            this.page.setOrderByField(sidx);
//            this.page.setAsc("ASC".equalsIgnoreCase(order));
        }

    }

    public Page<T> getPage() {
        return page;
    }

    public int getCurrPage() {
        return currPage;
    }

    public int getLimit() {
        return limit;
    }
}
